#include "workermanage.h"

using namespace std;

//菜单
int workManage::Menu(void)
{
    int choose=0;
    cout<<"*****欢迎进入职工管理系统*****"<<endl;
    cout<<"*******0.退出管理系统*******"<<endl;
    cout<<"*******1.增加职工信息*******"<<endl;
    cout<<"*******2.显示职工信息*******"<<endl;
    cout<<"*******3.删除离职职工*******"<<endl;
    cout<<"*******4.修改职工信息*******"<<endl;
    cout<<"*******5.查找职工信息*******"<<endl;
    cout<<"*******6.按照编号排序*******"<<endl;
    cout<<"*******7.清空所有文档*******"<<endl;
    cin>>choose;
    cout << "\033c";
    return choose;
}

//增加职工信息
void workManage::AddInfo(void)
{
    int id=0;
    string name="";
    int post=0;
    int num=0;
    cout<<"请输入您想增加的人数(输入0返回):"<<endl;
    cin>>num;
    if (num>0)
    {
        ofstream afs;
        afs.open("person.txt",ios::app);
        for (int i = 0; i < num; i++)
        {
            cout<<"请输入第"<<i+1<<"个职工的信息:"<<endl;
            cout<<"职工编号:"<<endl;
            cin>>id;
            cout<<"姓名:"<<endl;
            cin>>name;
            person *p=NULL;
            while (1)
            {
                cout<<"请选择该职工的岗位:"<<endl;
                cout<<"1.普通职工"<<endl;
                cout<<"2.经理"<<endl;
                cout<<"3.老板"<<endl;
                cin>>post;
                if (post==1)
                {
                    p = new worker(id,name,1);
                    break;
                }
                else if(post==2)
                {
                    p = new manager(id,name,2);
                    break;
                }
                else if(post==3)
                {
                    p = new boss(id,name,3);
                    break;
                }
                else
                {
                    cout<<"输入非法请重新输入"<<endl;
                }
            }
            afs<<p->id<<" "<<p->name<<" "<<p->post<<endl;
        }
        afs.close();
        
    }
    else if(num==0)
    {
        return;
    }
    else
    {
        cout<<"输入的人数不合法!"<<endl;
    }
}

//显示职工信息
void workManage::ShowInfo(void)
{
    ifstream ifs;
    ifs.open("person.txt", ios::in);

    if (!ifs.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }
    int id=0;
    string name="";
    int post=0;

    while (ifs>>id && ifs>>name && ifs>>post)
    {
        person *p=NULL;
        if (post==1)
        {
            p = new worker(id,name,1);
        }
        else if(post==2)
        {
            p = new manager(id,name,2);
        }
        else if(post==3)
        {
            p = new boss(id,name,3);
        }
        p->ShowInfo();
    }
    
    ifs.close();
}

//删除离职职工
void workManage::DeleteInfo(void)
{
    ifstream ifs1;
    ifs1.open("person.txt", ios::in);
    if (!ifs1.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }

    ofstream ofs1;
    ofs1.open("temp.txt",ios::out);
    if (!ofs1.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }

    int id=0;
    string name="";
    int post=0;
    int did=0;

    cout<<"请输入您想删除的职工编号:"<<endl;
    cin>>did;

    while (ifs1>>id>>name>>post)
    {
        if(did==id)
        {
            continue;
        }
        ofs1<<id<<" "<<name<<" "<<post<<endl;
    }

    ifs1.close();
    ofs1.close();
     
    ifstream ifs2;
    ifs2.open("temp.txt", ios::in);
    if (!ifs2.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }

    ofstream ofs2;
    ofs2.open("person.txt",ios::out);
    if (!ofs2.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }
    
    while (ifs2>>id>>name>>post)
    {
        ofs2<<id<<" "<<name<<" "<<post<<endl;
    }
    
    cout<<"删除成功"<<endl;
    ifs2.close();
    ofs2.close();
    
}

//修改职工信息
void workManage::ModifyInfo(void)
{
    ifstream ifs1;
    ifs1.open("person.txt", ios::in);
    if (!ifs1.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }

    ofstream ofs1;
    ofs1.open("temp.txt",ios::out);
    if (!ofs1.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }

    int id=0;
    string name="";
    int post=0;
    int mid=0;
    int tid=0;
    string mname="";
    int mpost=0;
    person *p=NULL;

    cout<<"请输入您想修改的职工编号:"<<endl;
    cin>>tid;
    cout<<"请输入新的职工编号:"<<endl;
    cin>>mid;
    cout<<"请输入新的职工姓名:"<<endl;
    cin>>mname;    
    cout<<"请选择该职工的岗位:"<<endl;
    cout<<"1.普通职工"<<endl;
    cout<<"2.经理"<<endl;
    cout<<"3.老板"<<endl;
    cin>>mpost;


    while (ifs1>>id>>name>>post)
    {
        if(tid==id)
        {
            
            ofs1<<mid<<" "<<mname<<" "<<mpost<<endl;
            continue;
        }   
        ofs1<<id<<" "<<name<<" "<<post<<endl;
    }

    ifs1.close();
    ofs1.close();
     
    ifstream ifs2;
    ifs2.open("temp.txt", ios::in);
    if (!ifs2.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }

    ofstream ofs2;
    ofs2.open("person.txt",ios::out);
    if (!ofs2.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }
    
    while (ifs2>>id>>name>>post)
    {
        ofs2<<id<<" "<<name<<" "<<post<<endl;
    }
    
    cout<<"修改成功"<<endl;
    ifs2.close();
    ofs2.close();
}

//清空所有文档
void workManage::ClearInfo(void)
{
    int choose=0;
    cout<<"您确定要清空所有文档,清空请按1,不清空请按其他键:"<<endl;
    cin>>choose;
    if (choose==1)
    {
        ofstream ofs;
        ofs.open("person.txt", ios::out);
        if (!ofs.is_open())
        {
            cout << "文件打开失败" << endl;
            return;
        }
        cout<<"清空完毕"<<endl;
        ofs.close();
    }
}
